package com.zc.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.edu.entity.Course;
import com.zc.edu.entity.vo.CourseInfoVo;
import com.zc.edu.entity.vo.CoursePublishVo;
import com.zc.edu.entity.vo.CourseQuery;
import com.zc.edu.service.CourseService;
import com.zc.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-11-25
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class CourseController {
	@Autowired
	private CourseService courseService;

	// 课程列表
	@GetMapping("/getCourseList")
	public R getCourseList() {
		List<Course> courseList = courseService.list(null);
		return R.ok().data("courseList", courseList);
	}

	// 分页查询课程
	@ApiOperation("分页查询课程")
	@GetMapping("/pageListCourse/{currentPage}/{size}")
	public R pageListCourse(@ApiParam(name = "currentPage", value = "当前页")@PathVariable("currentPage")long currentPage,
	                         @ApiParam(name = "size", value = "每页记录数")@PathVariable("size")long size) {
		Page<Course> pageTeacher = new Page<>(currentPage, size);
		courseService.page(pageTeacher, null);
		long total = pageTeacher.getTotal();
		List<Course> records = pageTeacher.getRecords();
		return R.ok().data("total", total).data("records", records);
	}

	@ApiOperation("带条件的分页查询讲师")
	@PostMapping("/pageListCourseCondition/{currentPage}/{size}")
	public R pageListCourseCondition(@ApiParam(name = "currentPage", value = "当前页")@PathVariable("currentPage")long currentPage,
	                                  @ApiParam(name = "size", value = "每页记录数")@PathVariable("size")long size,
	                                  @RequestBody(required = false) CourseQuery courseQuery) {
		Page<Course> pageCourse = new Page<>(currentPage, size);
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		String subjectParentId = courseQuery.getSubjectParentId();
		String subjectId = courseQuery.getSubjectId();
		String title = courseQuery.getTitle();
		String teacherId = courseQuery.getTeacherId();
		wrapper.eq(!StringUtils.isEmpty(subjectParentId), "subject_parent_id", subjectParentId)
				.eq(!StringUtils.isEmpty(subjectId), "subject_id", subjectId)
				.like(!StringUtils.isEmpty(title), "title", title)
				.eq(!StringUtils.isEmpty(teacherId), "teacher_id", teacherId);

		courseService.page(pageCourse, wrapper);
		long total = pageCourse.getTotal();
		List<Course> records = pageCourse.getRecords();
		return R.ok().data("total", total).data("records", records);
	}

	// 添加课程基本信息
	@PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
		String courseId = courseService.addCourseInfo(courseInfoVo);
    	return R.ok().data("id", courseId);
    }

    //根据课程id查询课程信息
	@GetMapping("/getCourseInfo/{courseId}")
	public R getCourseInfo(@PathVariable String courseId) {
		CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
		return R.ok().data("courseInfo", courseInfoVo);
	}

	// 更新课程信息
	@PostMapping("/updateCourseInfo")
	public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
		boolean isSuccess = courseService.updateCourseInfo(courseInfoVo);
		return isSuccess ? R.ok() : R.error();
	}

	// 查询课程展示信息
	@GetMapping("/getCoursePublishInfo/{courseId}")
	public R getCoursePublishInfo(@PathVariable String courseId) {
		CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(courseId);
		return R.ok().data("coursePublish", coursePublishVo);
	}

	// 修改课程发布状态
	@PostMapping("/modifyCourseStatus/{courseId}")
	public R modifyCourseStatus(@PathVariable String courseId) {
		Course course = new Course();
		course.setId(courseId);
		course.setStatus("Normal");
		boolean update = courseService.updateById(course);
		return update ? R.ok() : R.error();
	}

	// 删除课程
	@DeleteMapping("/deleteCourse/{courseId}")
	public R deleteCourse(@PathVariable String courseId) {
		boolean remove = courseService.deleteCourse(courseId);
		return remove ? R.ok() : R.error();
	}
}

